package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Coach;
import com.situ.gym.pojo.query.CoachQuery;
import com.situ.gym.service.ICoachService;
import com.situ.gym.util.JwtUtil;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@RestController
@RequestMapping("/coach")
public class CoachController {
    @Autowired
    private ICoachService coachService;

    @GetMapping("/selectAll")
    public Result<List<Coach>> selectAll() {
        List<Coach> list = coachService.list();
        return Result.ok(list);
    }

    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    //@RequestMapping("/list")
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    //@RequestMapping(value = "/list", method = RequestMethod.GET)
    @GetMapping("/list")
    public Result<IPage<Coach>> list(CoachQuery coachQuery) {
        System.out.println("CoachController.list");
        IPage<Coach> page = coachService.list(coachQuery);
        return Result.ok(page);
    }

    // /deleteById?id=1
    // /deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        coachService.removeById(id);
        return Result.ok("删除成功");
    }

    // /deleteAll/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        //coachService.removeBatchByIds(Arrays.asList(ids));
        coachService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    /*@PostMapping("/add")
    public Result add(Coach coach) {
        System.out.println(coach);
        return Result.ok();
    }*/

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Coach coach) {
        System.out.println(coach);
        coachService.save(coach);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Coach coach = coachService.getById(id);
        return Result.ok(coach);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Coach coach) {
        System.out.println(coach);
        //coachService.saveOrUpdate(coach);
        coachService.updateById(coach);
        return Result.ok("更新成功");
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Coach coach = new Coach();
        coach.setId(id);
        coach.setStatus(status);
        coachService.updateById(coach);
        return Result.ok("修改成功");
    }
}

